<?php
	/**
	 * Created by JetBrains PhpStorm.
	 * User: alex
	 * Date: 06.08.13
	 * Time: 15:56
	 * To change this template use File | Settings | File Templates.
	 */
	class Controller_Sendcart
	{

		function __construct()
		{
			$id = (int) $_GET["id"];
			$this->getProductInfo($id);
		}
		protected
		function getProductInfo($id)
		{
			$productInCookie = $_COOKIE['cart'];
			$productList = $productInCookie == NULL? $id: $productInCookie . "," . $id;

			$model = new Model_Cart();
			$cartInfo = $model->selectProductInCart($productList);
            $productInCart = explode(",", $productList);
            $cart['count'] = 0;
            $cart["sum"] = 0;
            foreach ($productInCart as $productId)
            {
                foreach ($cartInfo as $productInfo)
                {
                    if ($productInfo["id"] == $productId)
                    {
                        $cart["sum"] = $cart["sum"] + $productInfo["Price"];
                        ++$cart['count'];
                    }
                }
            }

			setcookie('cart', $productList, time()+3600);
			setcookie('cartInfo', serialize($cart), time()+3600);
			die (View_View::prepareSmallCartInfo($cart));
		}
	}
